package com.idis.managerApp.controller;

import com.idis.common.base.BaseController;
import com.idis.entity.Work;
import com.idis.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Ming on 2018/1/30.
 */
@RestController
@RequestMapping("/api/admin/work")
@Api(tags = "作品接口")
public class WorkController extends BaseController{

    @Autowired
    WorkService workService;

    @ApiOperation("获取所有作品信息")
    @GetMapping("/all")
    public Map findAll(){
        return sendDate(workService.findAll());
    }

    @ApiOperation("根据作品ID搜索作品")
    @GetMapping("/id/{id}")
    public Map findById(@ApiParam("作品ID")@PathVariable String id){
        Long numId = Long.parseLong(id);
        return sendDate(workService.findById(numId)) ;
    }

    @ApiOperation("更新作品")
    @PostMapping("/update")
    public Map update(@ApiParam("作品信息")Work work){
        Map map=new HashMap();
        if(work.getId().equals("")||work.getName().equals("")){
            sendNull(map);
            return map;
        }
        addResult(map, String.valueOf(workService.update(work)));
        addMessage(map,"修改成功");
        return map;
    }

    @ApiOperation("新增作品")
    @PostMapping("/save")
    public Map save(@ApiParam("作品信息")Work work){
        System.out.println(work.getPicture());
        Map map=new HashMap();
        if(work.getName().equals("")){
            sendNull(map);
            return map;
        }
        addResult(map, String.valueOf(workService.savaWork(work)));
        addMessage(map,"保存成功");
        return map;
    }

    @ApiOperation("获取作品类型及其代号")
    @GetMapping("/roles")
    public Map getRole(){
        return sendDate(workService.getRole());
    }

    @ApiOperation("删除作品")
    @PostMapping("/delete")
    public Map delete(@ApiParam("作品信息,需要且仅需要作品ID")Work work){
        Map map=new HashMap();
        addResult(map, String.valueOf(workService.delete(work)));
        addMessage(map,"删除成功");
        return map;
    }

}
